import 'dart:io';
import 'dart:math' hide log;
import 'dart:developer';
import 'package:baidu_trace/call_back.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:baidu_trace/baidu_trace.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _baiduTracePlugin = BaiduTrace();

  @override
  void initState() {
    super.initState();
    initPlatformState();
    _baiduTracePlugin.setListener();
    _baiduTracePlugin.getStream().listen((event) {
      log('listen----${event.method}');
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _baiduTracePlugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              TextButton(
                  onPressed: () => _baiduTracePlugin.initSdk({'keepAlive':false,'serviceId':228484,'ak':Platform.isAndroid?'UsCk1e8RjRDf7HHZLemM2CxwcYA1vlNC':'r3BZZbOhaeNZtMk2rhqYrr6424tlxS9w','mcode':'com.keyi.kydys'}),
                  child: const Text('初始化')),
              TextButton(
                  onPressed: () => _baiduTracePlugin.addEntity(
                      Random().nextInt(100), 228484, "111111112",
                      entityDesc: '测试'),
                  child: const Text('添加实体')),
              TextButton(
                  onPressed: () => _baiduTracePlugin.updateEntity(
                      Random().nextInt(100), 228484, "111111112",
                      entityDesc: '测试2'),
                  child: const Text('更新实体')),
              TextButton(
                  onPressed: () =>
                      _baiduTracePlugin.startTrace(228484, "111111112"),
                  child: const Text('开启服务')),
              TextButton(
                  onPressed: () => _baiduTracePlugin.startGather(),
                  child: const Text('开始上报')),
              TextButton(
                  onPressed: () => _baiduTracePlugin.stopGather(),
                  child: const Text('停止上报')),
              TextButton(
                  onPressed: () =>
                      _baiduTracePlugin.stopTrace(228484, "111111112"),
                  child: const Text('停止服务')),
            ],
          ),
        ),
      ),
    );
  }
}
